import { Command } from '../constant';
import Taro from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';
import api from 'api';
export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    async getMemberBase() {
      const result = await api.MemberController.getMemberBase();
      let memberCardType = 'noMember';
      if (result.isAquamarineMember) {
        memberCardType = 'lanjing';
      }
      if (result.isCrystalMember) {
        memberCardType = 'chijing';
      }
      if (result.isAquamarineMember && result.isCrystalMember) {
        memberCardType = 'shuangchong';
      }
      dispatch({
        type: Command.init,
        payload: {
          main: {
            memberCardType,
            memberInfo: result,
          },
        },
      });
      return memberCardType;
    },
    loadUserInfo() {
      let itemStr = Taro.getStorageSync('b2b-wechat@login');
      if (itemStr.user) {
        dispatch({
          type: Command.init,
          payload: {
            main: {
              customer: itemStr.user,
            },
          },
        });
      } else {
        Taro.navigateTo({
          url: '/pages/package-C/login/index',
        });
      }
    },
    async nextPage() {
      const {
        main: { searchForm, list },
      } = getData();
      const JSONParams = JSON.parse(JSON.stringify(searchForm));
      JSONParams.pageNum++;
      const result = await api.ActivationRecordController.getOpenMemberList({
        ...JSONParams,
      });
      action.commonChange('main.list', [...list, ...result.content]);
      let type = 'more';
      if (result.content.length <= 0) {
        type = 'end';
      }
      if (result.content.length <= 0 && list.length <= 0) {
        type = 'last';
      }
      action.commonChange('main.type', type);
      action.commonChange('main.searchForm.pageNum', JSONParams.pageNum);
    },
    async page() {
      const {
        main: { searchForm, list },
      } = getData();
      const JSONParams = JSON.parse(JSON.stringify(searchForm));
      JSONParams.pageNum++;
      const result = await api.ActivationRecordController.getOpenMemberList({
        ...JSONParams,
      });
      action.commonChange('main.list', [...list, ...result.content]);
      let type = 'more';
      if (result.content.length <= 0) {
        type = 'end';
      }
      if (result.content.length <= 0 && list.length <= 0) {
        type = 'last';
      }
      action.commonChange('main.type', type);
      action.commonChange('main.searchForm.pageNum', JSONParams.pageNum);
    },
    /**
     * 初始化
     */
    async query() { },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageAactivationRecordMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
